package batch;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * 批量消息-生产者
 *
 * @author Administrator
 * @date 2022/04/01 11:45
 **/
@Slf4j
public class BatchProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("Batch_Producer");

        producer.setNamesrvAddr("rocketmq1.cn:9876;rocketmq2.cn:9876");

        producer.start();

        ArrayList<Message> messages = new ArrayList<>();
        for (int i = 0; i < 100 ; i++) {
            messages.add(new Message("Batch_Msg", "TagA", RandomUtil.randomString(100).getBytes(StandardCharsets.UTF_8)));


        }
        SendResult result = producer.send(messages);

        log.info("生产结果:{}",result);

        producer.shutdown();


    }
}
